import { createRouter, createWebHashHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import login from "@/views/login/index.vue";
import main from "@/views/main/index.vue";
// import main from "@/views/main/ai-processing/ai-processing.vue";
import { getToken, clearToken } from "@/utils/auth";
import aiProcessing from "@/views/main/ai-processing/ai-processing.vue";
import dataServerse from "@/views/main/data-serverse/index.vue";
import changedetection from "@/views/main/change-detection/index.vue";

// import aiProcessing from "@/views/main/ai-processing/index.vue";
import dataManagement from "@/views/main/data-management/index.vue";
import editPwd from "@/views/main/ai-processing/edit-pwd.vue";
import accountInfo from "@/views/main/ai-processing/account-info.vue";
import analysis from "@/views/main/ai-analysis/index.vue";
const routes = [];
routes.push(
	{
		path: "/",
		redirect: "/main",
	},
	{ path: "/login", component: login },
	{
		path: "/main",
		component: main,
		redirect: "/main/ai-dataManage",
		children: [
			{
				path: "ai-processing",
				component: aiProcessing,
				children: [
					{
						path: "edit-pwd",
						component: editPwd,
					},
					{
						path: "account-info",
						component: accountInfo,
					},
				],
			},
			{
				path: "ai-analysis",
				component: analysis,
			},
			{
				path: "ai-bianhua",
				component: changedetection,
			},
			{
				path: "ai-management",
				component: dataManagement,
			},
			{
				path: "ai-dataManage",
				component: dataServerse,
			},
			{
				path: "ai-system",
				component: () => import("@/views/main/system/index.vue"),
				redirect: "/main/ai-system/user",
				// 三个子路由 用户管理 user.vue 角色管理 role.vue 操作日志 log.vue
				children: [
					{
						path: "user",
						component: () => import("@/views/main/system/user.vue"),
					},
					{
						path: "role",
						component: () => import("@/views/main/system/role.vue"),
					},
					{
						path: "log",
						component: () => import("@/views/main/system/log.vue"),
					},
				],
			},
		],
	}
);
//导入生成的路由数据
const router = createRouter({
	history: createWebHashHistory(),
	routes,
});

/** 路由白名单 */
const whiteList = ["/login"];
// router.beforeEach(async (_to, _from, next) => {
// 	NProgress.start();
// 	/* has token*/

// 	if (getToken()) {
// 		if (whiteList.indexOf(_to.path) !== -1) {
// 			next({
// 				path: "/main",
// 			});
// 		} else {
// 			next();
// 		}
// 	} else {
// 		// 没有token，说明没有登录，无法直接跳转到主页，需要跳转到登录页
// 		if (whiteList.indexOf(_to.path) !== -1) {
// 			// 在免登录白名单，直接进入
// 			next();
// 		}
// 		// 没有访问权限的其他页面被重定向到登录页面。
// 		else {
// 			clearToken();
// 			next(`/login`);
// 		}
// 	}
// });

router.afterEach((_to) => {
	NProgress.done();
});

export default router;
